package
{
	import feathers.controls.text.BitmapFontTextRenderer;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	import starling.textures.Texture;
 		import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
	import flash.system.System;
	/**
	 * preloader of the application
	 */
    [SWF(width = "836", height = "557", frameRate = "60", backgroundColor = "#ffffff")]
	
	public class Preloader extends MovieClip
	{
		private const STARTUP_CLASS		:String = "Main";
		
		[Embed(source = "../media/graphics/load1.png")]
		public static const load1 :Class;
		[Embed(source = "../media/graphics/load2.png")]
		public static const load2 :Class;
		[Embed(source = "../media/graphics/load3.png")]
		public static const load3 :Class;
		private var load1:Bitmap = new load1 ();
		private var load2:Bitmap = new load2 ();
		private var load3:Bitmap = new load3 ();
		private var tx:Sprite;
        private var tx1:Sprite;
		private var m_text:TextField;
		private var m_text1:TextField;
		public function Preloader()
		{	
			tx1 = new Sprite();
			tx1.addChild(load3);
			this.addChild(tx1);
			tx1.x = 308;
			tx1.y = 280;
			
			tx = new Sprite();
			tx.addChild(load2);
			this.addChild(tx);
			tx.x = -136;
			tx.y = 280;
			
			
			
			m_text = new TextField();
			m_text.width = 500;
			m_text.x = 425;
			m_text.y = 325;
			
			m_text1 = new TextField();
			m_text1.width = 500;
			m_text1.x = 370;
			m_text1.y = 600;
			
			addChild(load1);

			this.addChild(m_text1);
			this.addChild(m_text);
			addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
			stop();
			this.scaleX = 836 / 1050;
			this.scaleY = this.scaleX;
		}
 
        private function onAddedToStage(event:Event):void
        {
			var textformat:TextFormat = new TextFormat();
			// Set the desired font family. It is important to embed
			// this font family in the library as a font object and export
			// it for ActionScript.
			textformat.font = "Time New Roman";
			// 12 points font size.
			textformat.size = 30;
			// Set text color to red.
			textformat.color = 0xFFFFFF; // red text
			m_text1.defaultTextFormat = textformat;
			
			textformat.size = 15;
			m_text.defaultTextFormat = textformat;
			
			m_text1.text = "Tải dữ liệu hình ảnh";
			
			
			
			
            stage.scaleMode = StageScaleMode.SHOW_ALL;
            stage.align 	= StageAlign.TOP_LEFT;
 
			addEventListener( Event.ENTER_FRAME, onEnterFrame );
            removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
		}
 
        private function onEnterFrame(event:Event):void
        {
            var bytesLoaded:int = root.loaderInfo.bytesLoaded;
            var bytesTotal:int  = root.loaderInfo.bytesTotal;
 
			m_text.text="loading: "+bytesLoaded+" / "+bytesTotal;
			// update your progress bar here
			tx.x = -125 + bytesLoaded / bytesTotal * 441;
            if( bytesLoaded >= bytesTotal )
            {
				
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                run();
            }
		}
 
		private function run():void
        {
			nextFrame();
			
			var startupClass:Class = getDefinitionByName(STARTUP_CLASS) as Class;
            if( startupClass == null )
				throw new Error("Invalid Startup class in Preloader: " + STARTUP_CLASS);
 
			var startupObject:DisplayObject = new startupClass() as DisplayObject;
			if( startupObject == null )
				throw new Error("Startup class needs to inherit from Sprite or MovieClip.");
 
			addChildAt( startupObject, 0 );
			this.removeChild(load1);
			this.removeChild(m_text);
			this.removeChild(tx);
			this.removeChild(tx1);
			this.removeChild(m_text1);
		}
	}
}